// 异步操作或者 对 mutation 封装
import { playMode } from 'common/js/config';
import { shuffle } from 'common/js/util';
import { saveSearch, deleteSearch, clearSearch, savePlay, saveFavorite, deleteFavorite } from 'common/js/cache';

function findIndex(list, song) {
  return list.findIndex((item) => item.id === song.id);
}

// 选择一首歌曲
export const selectPlay = ({ commit, state }, { list, index }) => {
  commit('setSequenceList', list);
  if (state.mode === playMode.random) {
    let randomList = shuffle(list);
    commit('setPlaylist', randomList);
    index = findIndex(randomList, list[index]);
  } else {
    commit('setPlaylist', list);
  }
  commit('setCurrentIndex', index);
  commit('setFullScreen', true);
  commit('setPlayingState', true);
};

// 随机播放列表
export const randomPlay = ({ commit }, list) => {
  commit('setPlayMode', playMode.random);
  commit('setSequenceList', list);
  commit('setPlaylist', shuffle(list));
  commit('setCurrentIndex', 0);
  commit('setFullScreen', true);
  commit('setPlayingState', true);
};

// 添加一首歌
export const insertSong = ({ commit, state }, song) => {
  let playlist = state.playlist.slice();
  let sequenceList = state.sequenceList.slice();
  let currentIndex = state.currentIndex;
  // 记录当前歌曲
  let currentSong = playlist[currentIndex];
  // 查找当前列表中是否有待插入的歌曲并返回其索引
  let fpIndex = findIndex(playlist, song);
  // 因为是插入歌曲，所以索引+1
  currentIndex++;
  // 插入这首歌到当前索引位置
  playlist.splice(currentIndex, 0, song);
  // 如果已经包含了这首歌
  if (fpIndex > -1) {
    // 如果当前插入的序号大于列表中的序号
    if (currentIndex > fpIndex) {
      playlist.splice(fpIndex, 1);
      currentIndex--;
    } else {
      playlist.splice(fpIndex + 1, 1);
    }
  }
  let currentSIndex = findIndex(sequenceList, currentSong) + 1;
  let fsIndex = findIndex(sequenceList, song);
  sequenceList.splice(currentSIndex, 0, song);
  if (fsIndex > -1) {
    if (currentSIndex > fsIndex) {
      sequenceList.splice(fsIndex, 1);
    } else {
      sequenceList.splice(fsIndex + 1, 1);
    }
  }
  commit('setPlaylist', playlist);
  commit('setSequenceList', sequenceList);
  commit('setCurrentIndex', currentIndex);
  commit('setFullScreen', true);
  commit('setPlayingState', true);
};

export const deleteSong = function({ commit, state }, song) {
  let playlist = state.playlist.slice();
  let sequenceList = state.sequenceList.slice();
  let currentIndex = state.currentIndex;
  let pIndex = findIndex(playlist, song);
  playlist.splice(pIndex, 1);
  let sIndex = findIndex(sequenceList, song);
  sequenceList.splice(sIndex, 1);
  // 删除的是最后一首歌以及当前播放的歌在删除的歌曲后面
  if (currentIndex > pIndex || currentIndex === playlist.length) {
    currentIndex--;
  }
  commit('setPlaylist', playlist);
  commit('setSequenceList', sequenceList);
  commit('setCurrentIndex', currentIndex);
  if (!playlist.length) {
    commit('setPlayingState', false);
  } else {
    commit('setPlayingState', true);
  }
};

export const deleteSongList = function({ commit }) {
  commit('setCurrentIndex', -1);
  commit('setSequenceList', []);
  commit('setPlaylist', []);
  commit('setPlayingState', false);
};

export const showToast = ({ commit, state }, text) => {
  if (state.toast.timer !== null) {
    clearTimeout(state.toast.timer);
    commit('updateToast', { show: false, text: '', timer: null });
  }
  commit('updateToast', {
    show: true,
    text,
    timer: setTimeout(() => {
      commit('updateToast', { show: false, text: state.toast.text, timer: null });
    }, 3200),
  });
};

// 保存搜索结果
export const saveSearchHistory = ({ commit, state }, query) => {
  commit('setSearchHistory', saveSearch(query));
};

// 删除一条搜索历史
export const deleteSearchHistory = ({ commit, state }, query) => {
  commit('setSearchHistory', deleteSearch(query));
};

// 清空搜索记录
export const clearSearchHistory = function({ commit }) {
  commit('setSearchHistory', clearSearch());
};

// 添加播放历史
export const savePlayHistory = function({ commit }, song) {
  commit('setPlayHistory', savePlay(song));
};

export const saveFavoriteList = function({ commit }, song) {
  commit('setFavoriteList', saveFavorite(song));
};

export const deleteFavoriteList = function({ commit }, song) {
  commit('setFavoriteList', deleteFavorite(song));
};
